package com.htt.app.doctruyendemkhuya_joyfm.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.htt.app.doctruyendemkhuya_joyfm.AboutActivity;
import com.htt.app.doctruyendemkhuya_joyfm.R;
import com.htt.app.doctruyendemkhuya_joyfm.config.Config;
import com.htt.app.doctruyendemkhuya_joyfm.object.Custom;
import com.htt.app.doctruyendemkhuya_joyfm.utils.Utils;

public class ListCustomAdapter extends ArrayAdapter<Custom>{
	
	private Activity activity;
	private int resource;
	private static LayoutInflater inflater = null;
	
	public ListCustomAdapter(Activity context, int textViewResourceId,
			List<Custom> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.resource = textViewResourceId;
		this.activity = context;
		
	}
	
	public static class ViewHolder {
		ImageView image;
		TextView name;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		ViewHolder holder;
		
		if(convertView == null) {
			v = inflater.inflate(resource, null);
			holder = new ViewHolder();
			holder.image = (ImageView) v.findViewById(R.id.list_custom_item_img);
			holder.name = (TextView) v.findViewById(R.id.list_custom_item_name);
			v.setTag(holder);
		
		} else {
			holder = (ViewHolder) v.getTag();
		}
		
		Custom custom = getItem(position);
		holder.name.setText(custom.getName());
		holder.image.setImageDrawable(activity.getResources().getDrawable(custom.getResource()));
		
		v.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					startAbout();
					break;
				
				case 1:
					sendMail(Config.MAIL_SUPPORT);
					break;
				
				case 2:
					rateApp();
					break;
					
				case 3:
					share();
					break;
				
				case 4:
					showMoreApp();
					break;
					
				default:
					Toast.makeText(activity, R.string.app_developing, Toast.LENGTH_SHORT).show();
					break;
				}
				  
			}
		});
		
		return v;
	
	}
	
	/*
	 *  giá»›i thiá»‡u app Radio
	 */
	private void startAbout() {
		Intent intent = new Intent(activity, AboutActivity.class);
		activity.startActivity(intent);
	}
	
	/*
	 * gui mail 
	 */
	private void sendMail(String mailID) {
		  try {
		   if (Utils.haveNetworkConnection(activity)){
		    Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.fromParts("mailto", mailID, null));
		       emailIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.mail_title_send));
		       activity.startActivity(Intent.createChooser(emailIntent, activity.getString(R.string.mail_sending)));
		   }
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
		 }
	
	
	/*
	 * chia sáº» vá»›i báº¡n bÃ¨
	 */
	private void share() {
		String messageShare = "https://play.google.com/store/apps/details?id=" + activity.getApplicationContext().getPackageName();
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.putExtra(Intent.EXTRA_TEXT, messageShare);
		activity.startActivity(Intent.createChooser(shareIntent, 
				activity.getResources().getString(R.string.custom_share_title)));
	}
	
	/*
	 * Ä‘Ã¡nh giÃ¡ sáº£n pháº©m
	 */
	private void rateApp() {
		/* This code assumes you are inside an activity */
		final Uri uri = Uri.parse("market://details?id=" + activity.getApplicationContext().getPackageName());
		final Intent rateAppIntent = new Intent(Intent.ACTION_VIEW, uri);

		if (activity.getPackageManager().queryIntentActivities(rateAppIntent, 0).size() > 0)
		{
		    activity.startActivity(rateAppIntent);
		}
		else
		{
		    Toast.makeText(activity, R.string.app_rate_error, Toast.LENGTH_SHORT).show();
		}
	}
	
	/**
	 * show more app custom
	 */
	private void showMoreApp() {
		Uri uri = Uri.parse("market://search?q=pub:\"LuvSoft+Inc\"");
		Intent rateAppIntent = new Intent(Intent.ACTION_VIEW, uri);

		activity.startActivity(rateAppIntent);
	}
	
}
